package com.chinalwb.are.demo.slice;

import com.chinalwb.are.demo.ResourceTable;
import com.chinalwb.are.demo.toolitems.ARE_DefaultToolbarAbility;
import com.chinalwb.are.demo.*;
import com.chinalwb.are.utils.FindUtils;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Button;

/**
 * IndexAbilitySlice
 *
 * @since 2021/06/22
 */
public class IndexAbilitySlice extends AbilitySlice {
    @Override
    public void onStart(Intent intent) {
        DemoUtil.initStatus(getAbility());
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_index);
        initViews();
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    private void initViews() {
        Button defaultToolbarButton = FindUtils.findButtonById(getAbility(), ResourceTable.Id_defaultToolbar);
        openPage(defaultToolbarButton, ARE_DefaultToolbarAbility.class);

        Button fullBottomButton = FindUtils.findButtonById(getAbility(), ResourceTable.Id_fullBottomButton);
        openPage(fullBottomButton, ARE_FullBottomAbility.class);

        Button fullTopButton = FindUtils.findButtonById(getAbility(), ResourceTable.Id_fullTopButton);
        openPage(fullTopButton, ARE_FullTopAbility.class);

        Button minBottomButton = FindUtils.findButtonById(getAbility(), ResourceTable.Id_minBottomButton);
        openPage(minBottomButton, ARE_MinBottomAbility.class);

        Button minTopButton = FindUtils.findButtonById(getAbility(), ResourceTable.Id_minTopButton);
        openPage(minTopButton, ARE_MinTopAbility.class);

        Button minHideButton = FindUtils.findButtonById(getAbility(), ResourceTable.Id_minHideButton);
        openPage(minHideButton, ARE_MinHideAbility.class);

        Button multipleInstanceButton = FindUtils.findButtonById(getAbility(), ResourceTable.Id_multiInstanceButton);
        openPage(multipleInstanceButton, ARE_MultiInstanceAbility.class);
    }

    private void openPage(Button button, final Class ability) {
        button.setClickedListener(component -> {
            Intent intent = new Intent();
            Operation operation = new Intent.OperationBuilder()
                    .withBundleName("com.chinalwb.are.demo")
                    .withAbilityName(ability.getCanonicalName())
                    .build();
            intent.setOperation(operation);
            startAbility(intent);
        });
    }
}
